package xuegao.study.mall.admin.controller.exam;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xuegao.study.mall.admin.dto.study.exam.Exam;
import xuegao.study.mall.admin.feign.AskServiceClient;
import xuegao.study.mall.common.api.CommonResult;

@RestController
@RequestMapping(value = "/api/v1/exams")
public class ExamController {

    private final Logger LOGGER = LoggerFactory.getLogger(ExamController.class);

    @Autowired
    private AskServiceClient askServiceClient;

    @ApiOperation("试卷列表")
    @PostMapping(value = "/getAll")
    public CommonResult<JSONObject> getAllExams(@RequestBody JSONObject data) {
        data.put("Source", "0");
        String body = askServiceClient.getAllExams(data);
        LOGGER.info("试卷列表查询|data:{}, body:{}", data, body);
        JSONObject jsonObject = JSONObject.parseObject(body);
        return CommonResult.success(jsonObject);
    }

    @ApiOperation("试卷详情")
    @PostMapping(value = "/detail/{id}")
    public CommonResult<JSONObject> getExamDetail(@PathVariable("id") String id) {
        String body = askServiceClient.getExamDetail(id);
        JSONObject jsonObject = JSONObject.parseObject(body);
        return CommonResult.success(jsonObject);
    }

    @ApiOperation("添加或修改试卷")
    @PostMapping(value = "/add")
    public CommonResult<String> createExam(@RequestBody Exam exam) {
        System.out.println("quest:"+JSONObject.toJSONString(exam));
        LOGGER.info("添加或修改试卷|params:{}", JSONObject.toJSONString(exam));
        String body = askServiceClient.createExam(exam);
        LOGGER.info("添加或修改试卷|body:{}", body);
        return CommonResult.success(body);
    }

    @ApiOperation("删除试卷")
    @PostMapping(value = "/delete/{id}")
    public CommonResult<String> deleteExam(@PathVariable("id") String id) {
        askServiceClient.deleteExam(id);
        return CommonResult.success(id);
    }

    @ApiOperation("发布试卷")
    @PostMapping(value = "/publish/{id}")
    public CommonResult<String> publishExam(@PathVariable("id") String id) {
        askServiceClient.publishExam(id);
        return CommonResult.success(id);
    }
}
